<?php

require_once ("includes/connect.php");
require_once ("includes/function.php");

$id = $_REQUEST["id"];

$db = new myDBS();
$Cur = new Currency;

$sql = "SELECT tbl_promise_budget.*, panngan2_name  "
        . " FROM tbl_promise_budget, panngan2 "
        . " WHERE id = '$id' "
        . " AND tbl_promise_budget.panngan2_id = panngan2.panngan2_id ";


//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->numPages = $this->setSourceFile('form/promise1.pdf');
            //$this->numPages = $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
//$pdf = new PDF();
$pdf = new PDF($orientation = 'P', $unit = 'mm', 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabun', '', 16);
// now write some text above the imported page

//$pdf->SetXY(35, 35);
//$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");

if ($rows->card_id2 <> "" || $rows->card_id3 <> "") {
    $party= "และคณะ";
}
$pdf->SetXY(35, 27);
$name = get_name_person($rows->card_id1);
$pdf->Write(0, $name . " " . $party);

$pdf->SetXY(125, 27);
$position = get_position($rows->card_id1);
$pdf->Write(0, $position );

//$pdf->SetXY(125, 56);
//$pdf->Write(0, "อุดรธานี");

//$date = date_create($rows->pet_date);
//$pdf->Write(0, date_format($date, "d / m / Y"));

//$pdf->SetXY(55, 64);
//$pdf->Write(0, $rows->panngan2_name);

$pdf->SetXY(50, 49);
$pdf->Write(0, $rows->reason);

$pdf->SetXY(130, 88);
$pdf->Cell(53, 0, number_format($rows->total, 2), 0, 0, 'R');
$pdf->SetXY(130, 66);
$pdf->Cell(53, 0, number_format($rows->total, 2), 0, 0, 'R');

$pdf->SetXY(25, 87);
$pdf->Cell(84, 0, $Cur->bahtThai($rows->total), 0, 0, 'C');

$pdf->SetXY(70, 162);
$pdf->Cell(35, 0, number_format($rows->total, 2), 0, 0, 'C');

$pdf->SetXY(100, 162);
$pdf->Cell(84, 0, $Cur->bahtThai($rows->total), 0, 0, 'C');

$pdf->SetXY(73, 215);
$pdf->Cell(35, 0, number_format($rows->total, 2), 0, 0, 'C');

$pdf->SetXY(110, 215);
$pdf->Cell(84, 0, $Cur->bahtThai($rows->total), 0, 0, 'C');

$pdf->SetXY(37, 245);
$pdf->Cell(50, 0, number_format($rows->total, 2), 0, 0, 'C');

$pdf->SetXY(90, 245);
$pdf->Cell(84, 0, $Cur->bahtThai($rows->total), 0, 0, 'C');

//$pdf->SetXY(125, 71);
//$date = date_create($rows->date_create);
//$pdf->Write(0, date_format($date, "d/m/Y"));
////$pdf->Write(0, $rows->date_create);
//
//$pdf->SetXY(69, 80);
//$pdf->Cell(70, 0, number_format($rows->inpNet, 2), 0, 0, 'C');
//
//$pdf->SetXY(29, 110);
////$pdf->Write(0, $rows->comment);
//$pdf->MultiCell(80, 75, $rows->comment, 0, '', 0, 1, '', '', true);
//
//$pdf->SetXY(115, 110);
//$pdf->Cell(25, 0, number_format($rows->inpValues, 2), 0, 0, 'R');
//
//$pdf->SetXY(167, 110);
//$pdf->Cell(20, 0, number_format($rows->inpNet, 2), 0, 0, 'R');
//
//$pdf->SetXY(115, 190);
//$pdf->Cell(25, 0, number_format($rows->inpValues, 2), 0, 0, 'R');
//
//$pdf->SetXY(167, 190);
//$pdf->Cell(20, 0, number_format($rows->inpNet, 2), 0, 0, 'R');


//$pdf->SetXY(200, 240);
//$pdf->Write(0, "(.................................................)");

/*
  $pdf->SetXY(25, 48);
  $pdf->Write(0, $rows->expend_type);

  $pdf->SetXY(87, 48);
  $pdf->Write(0, $rows->expend_sub_type);

  $pdf->SetXY(87, 48);
  $pdf->Write(0, $rows->expend_mini_type);
 */

/*
  $pdf->SetXY(116, 69);
  //$pdf->Write(0, $rows->comment, '', 0, 'J', true, 0, false, true, 0);
  $pdf->Cell(89,48, $rows->comment,0,	1,"L");

  $pdf->SetXY(73, 76);
  $pdf->Cell(41,0,number_format($rows->inpTax, 2),0,0,'R');

  $pdf->SetXY(73, 83);
  $pdf->Cell(41,0,number_format($rows->inpTotal, 2),0,0,'R');

  $pdf->SetXY(73, 90);
  $pdf->Cell(41,0,number_format($rows->WithholdingTax, 2),0,0,'R');

  $pdf->SetXY(73, 97);
  $pdf->Cell(41,0,number_format($rows->inpPenalty, 2),0,0,'R');

  $pdf->SetXY(73, 111);
  $pdf->Cell(41,0,number_format($rows->inpNet, 2),0,0,'R');

  $pdf->SetXY(53, 117.5);
  $pdf->Write(0, $Cur->bahtThai($rows->inpNet));


  $pdf->SetXY(20, 171);
  $pdf->Cell(39,0,number_format($rows->inpNet, 2),0,0,'C');

  $pdf->SetXY(88, 171);
  $pdf->Cell(39,0,number_format($rows->inpNet, 2),0,0,'C');

  $pdf->SetXY(155, 171);
  $pdf->Cell(39,0,number_format($rows->inpNet, 2),0,0,'C');

  $pdf->SetXY(29, 226);
  $pdf->Cell(36,0,number_format($rows->inpNet, 2),0,0,'C');

  $pdf->SetXY(70, 226);
  $pdf->Write(0, $Cur->bahtThai($rows->inpNet));

  $pdf->SetXY(27, 233);
  $pdf->Write(0, $rows->company_name);

  $pdf->SetXY(35, 247);
  $pdf->Cell(55,0,number_format($rows->inpNet, 2),0,0,'C');
 */
//$pdf->AddPage();

//if ($pdf->numPages > 1) {
//    for ($pages = 2; $pages <= $pdf->numPages; $pages++) {
//        $pdf->endPage();
//        $pdf->_tplIdx = $pdf->importPage($pages);
//        $pdf->AddPage();
//
//        if ($pages == 2) {
//            $pdf->SetXY(100, 65);
//            $pdf->Write(0, $rows->lvl1_name);
//
//            $pdf->SetXY(165, 65);
//            $pdf->Write(0, $rows->lvl2_name);
//
//            $pdf->SetXY(20, 72);
//            $pdf->Write(0, $rows->expenses1_name);
//
//            $pdf->SetXY(90, 72);
//            $pdf->Write(0, $rows->p_id);
//
//            $pdf->SetXY(25, 79);
//            $pdf->Write(0, $rows->expenses2_name);
//
//            //$pdf->SetXY(35, 100);
//            //$pdf->Cell(55,0,"adsfdsafasdfad",0,0,'C');
//        }
//    }
//}


$pdf->Output('output_form1.pdf', 'I');
?>